package ProfileParser;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.res.AssetManager;
                                                                                                                                                                                                                                                                                                                                                           
import AC.Controller.ACCActivity;
import ClimateControlProcessor.Process;
import ClimateControlProcessor.ClimateObjects.BlowerMotor;
import ClimateControlProcessor.ClimateObjects.Constant;
import ClimateControlProcessor.ClimateObjects.Constant.FANDIRECTION;
import ClimateControlProcessor.ClimateObjects.CurrentVehicleStatus;
import ClimateControlProcessor.ClimateObjects.Constant.MODE;
import ClimateControlProcessor.ClimateObjects.Temperature;

public class ProfileParser {	
	public ProfileParser(){		
	}

	public void loadProfile(String username)
	{		
		try{			
			  // Open the file that is the first 
			  // command line parameter
			 AssetManager am = ACCActivity.getAppContext().getAssets();	
			 // FileInputStream fstream = new FileInputStream(Constant.PROFILE);
			 InputStream fstream = am.open(Constant.PROFILE);
			  // Get the object of DataInputStream
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  String strLine;
			  String[] arrValues = null;
			  String[] arrDefault = null;
			  String[] arrUser = null;
			  //Read File Line By Line
			  while ((strLine = br.readLine()) != null){				  
				  arrValues = strLine.split(Constant.STR_POINT);
				  
				  if(Constant.DEFAULT.equalsIgnoreCase(arrValues[Constant.VALUE0])){					  
					  arrDefault = strLine.split(Constant.STR_POINT);					  
				  }
				  
				  if(username.equalsIgnoreCase(arrValues[Constant.VALUE0])){
					  arrUser = strLine.split(Constant.STR_POINT);
				  }
			  }
			  //Close the input stream
			  in.close();
			  
			  // Build up Climate Logic Objects
			  if(arrUser != null){				 
				  buildObjects(arrUser);
			  }else{			  
				  if(arrDefault != null){
					 
					  buildObjects(arrDefault);
				  }
			  }
	    }catch (Exception e){//Catch exception if any
	    }	  
	}
	
	private void buildObjects(String[] arrConfigValues){
		Constant.currentACConfig = new CurrentVehicleStatus();
		
		// set Mode
		if(Constant.AUTO.equalsIgnoreCase(arrConfigValues[Constant.VALUE1]))	
			Constant.currentACConfig.Mode = MODE.Auto;
			if(Constant.MANUAL.equalsIgnoreCase(arrConfigValues[Constant.VALUE1]))
				Constant.currentACConfig.Mode = MODE.Manual;
		if(Constant.DEBUG.equalsIgnoreCase(arrConfigValues[Constant.VALUE1]))
			Constant.currentACConfig.Mode = MODE.Debug;
		
		// set Temperature
		Constant.currentACConfig.currTemp = new Temperature();
		Constant.currentACConfig.currTemp.setCurrentTemp(Integer.parseInt(arrConfigValues[Constant.VALUE2]));	
		
		// set Fan Speed
		Constant.currentACConfig.currBlowMotor = new BlowerMotor();
		Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Integer.parseInt(arrConfigValues[Constant.VALUE3]));
		
		// set air direction
		Constant.currentACConfig.airDirection = matchFanDirection(arrConfigValues[Constant.VALUE4]);
	}
	
	private static FANDIRECTION matchFanDirection(String direction){
		for(FANDIRECTION dir: FANDIRECTION.values()){
			if(direction.equalsIgnoreCase(dir.name())){
				return dir;
			}	
		}
		return FANDIRECTION.HeadNFoot;	
	}
}
